using Gavel.Common.Utilities.Const;
using Gavel.Common.Utilities.Extension;

namespace Gavel.Common.Utilities.Model;

/// <summary>
/// 报文传输
/// </summary>
public class MessageTransmit
{
    /// <summary>
    /// 协议类型
    /// </summary>
    public ProtocolType Protocol { get; set; }
    
    /// <summary>
    /// 功能码
    /// </summary>
    public FunctionType Function { get; set; }

    /// <summary>
    /// 传输方向
    /// </summary>
    public TransmitWay TransmitWay { get; set; }

    /// <summary>
    /// 寄存器地址
    /// </summary>
    public ushort Address { get; set; }

    /// <summary>
    /// 报文数据
    /// </summary>
    public byte[] Data { get; set; }

    /// <summary>
    /// 构造方法
    /// </summary>
    /// <param name="protocol">协议类型</param>
    /// <param name="function">功能码</param>
    /// <param name="way">传输方向</param>
    /// <param name="address">寄存器地址</param>
    /// <param name="data">报文数据</param>
    public MessageTransmit(ProtocolType protocol, FunctionType function, TransmitWay way, ushort address, byte[] data)
    {
        Protocol = protocol;
        Function = function;
        TransmitWay = way;
        Address = address;
        Data = data;
    }

    /// <summary>
    /// 将报文解析为行消息
    /// </summary>
    public string Message => $"【{DateTime.Now:HH:mm:ss}】{(TransmitWay == TransmitWay.Request ? "发送：" : "接收：")}{Data.ToHex()}";
}